<template>
  <div class="artCate-page">
    <el-container>
      <el-header class="header">
        <span class="title">文章分类</span>
        <el-button
          @click="dialogFormVisible = true"
          class="btn"
          size="mini"
          type="primary"
          >添加分类</el-button
        >
      </el-header>
      <el-main>
        <el-table border style="width: 100%" :data="form">
          <el-table-column type="index" label="序号" width="50%">
          </el-table-column>
          <el-table-column prop="cate_name" label="分类名称"> </el-table-column>
          <el-table-column prop="cate_alias" label="分类别名">
          </el-table-column>
          <el-table-column prop="operate" label="操作">
            <el-button size="mini" type="primary">修改</el-button>
            <el-button size="mini" type="danger">删除</el-button>
          </el-table-column>
        </el-table>
      </el-main>
    </el-container>

    <!-- 弹框 -->
    <el-dialog title="新增分类" :visible.sync="dialogFormVisible">
      <el-form
        :model="list"
        label-position="right"
        label-width="80px"
        :rules="rules"
        ref="formRef"
      >
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="list.name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="alias">
          <el-input v-model="list.alias"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button @click="doAdd" type="primary">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { myCateList, myCateAdd } from '@/api/avatar'
export default {
  name: 'artCate-page',
  data () {
    return {
      list: {
        alias: '',
        name: '',
        id: Date.now()
      },
      form: [
        {
          cate_name: '',
          cate_alias: ''
        }
      ],
      rules: {
        name: [{ required: true, message: '请输入分类名称', trigger: 'blur' }],
        alias: [{ required: true, message: '请输入分类别名', trigger: 'blur' }]
      },
      dialogFormVisible: false
    }
  },
  async created () {
    this.CateList()
  },
  methods: {
    async CateList () {
      const res = await myCateList()
      console.log(res)
      this.form = res.data
    },
    async doAdd () {
      await this.$refs.formRef.validate()
      const res = await myCateAdd({
        cate_name: this.list.name,
        cate_alias: this.list.alias
      })
      console.log(res)
      this.CateList()
      this.dialogFormVisible = false
    }
  }
}
</script>

<style lang="scss" scoped>
.artCate-page {
  background-color: #fff;
  margin: 10px;

  .header {
    display: flex;
    justify-content: space-between;
    border-bottom: 1px solid #ebeef5;
    align-items: center;
    .title {
      font-size: 12px;
      padding: 7px 15px;
      border-radius: 3px;
    }
  }
  .el-dialog__body {
    padding: 0px 20px;
  }
}
</style>
